package cn.iocoder.yudao.module.erp.framework.web.config;

import cn.iocoder.yudao.module.erp.dal.dataobject.contractinfo.ContractInfoDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.customeraccount.CustomerAccountDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.monthplaninfo.MonthPlanInfoDO;
import cn.iocoder.yudao.module.erp.dal.mysql.contractinfo.ContractInfoMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.customerinfo.CustomerInfoMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.monthplaninfo.MonthPlanInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class ScheduledTaskService {
    @Autowired
    private ConcurrentHashMap<String, CustomerAccountDO> concurrentHashMap;

    @Autowired
    private ConcurrentHashMap<String, MonthPlanInfoDO> monthHashMap;


    @Autowired
    private ConcurrentHashMap<String, ContractInfoDO> contractHashMap;

    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    @Autowired
    private MonthPlanInfoMapper monthPlanInfoMapper;

    @Autowired
    private ContractInfoMapper contractInfoMapper;

    @Scheduled(fixedRate = 1000*5) // 每 1000 毫秒执行一次
    public void scheduledTask() {
        System.out.println("concurrentHashMap,,,,"+concurrentHashMap.size());
        System.out.println("monthHashMap,,,."+monthHashMap.size());
        System.out.println("contractHashMap,,,,"+contractHashMap.size());
        if(concurrentHashMap.size()>0){
            Iterator<ConcurrentHashMap.Entry<String, CustomerAccountDO>> iterator = concurrentHashMap.entrySet().iterator();
            while (iterator.hasNext()) {
                ConcurrentHashMap.Entry<String, CustomerAccountDO> entry = iterator.next();
                //String key=entry.getKey();
                CustomerAccountDO cdo=entry.getValue();
                //System.out.println("Processing key: " + entry.getKey() + ", value: " + entry.getValue());
                //处理数据
                customerInfoMapper.updateThisAccount(cdo);
                iterator.remove(); // 使用迭代器的 remove 方法安全地删除元素
            }
        }


        if(monthHashMap.size()>0){
            Iterator<ConcurrentHashMap.Entry<String, MonthPlanInfoDO>> iterator = monthHashMap.entrySet().iterator();
            while (iterator.hasNext()) {
                ConcurrentHashMap.Entry<String, MonthPlanInfoDO> entry = iterator.next();
                //String key=entry.getKey();
                MonthPlanInfoDO cdo=entry.getValue();
                //System.out.println("Processing key: " + entry.getKey() + ", value: " + entry.getValue());
                //处理数据
                monthPlanInfoMapper.updateThisAccount(cdo);
                iterator.remove(); // 使用迭代器的 remove 方法安全地删除元素
            }
        }


        if(contractHashMap.size()>0){
            Iterator<ConcurrentHashMap.Entry<String, ContractInfoDO>> iterator = contractHashMap.entrySet().iterator();
            while (iterator.hasNext()) {
                ConcurrentHashMap.Entry<String, ContractInfoDO> entry = iterator.next();
                //String key=entry.getKey();
                ContractInfoDO cdo=entry.getValue();
                //System.out.println("Processing key: " + entry.getKey() + ", value: " + entry.getValue());
                //处理数据
                contractInfoMapper.updateThisAccount(cdo);
                iterator.remove(); // 使用迭代器的 remove 方法安全地删除元素
            }
        }

    }
}
